/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.itu.modernai;

import java.util.Random;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
 *
 * @author Christoffer
 */
        
public class Gene implements Comparable{
    //Constants
    public static int CHROMOSOME_SIZE = 20;
    
    
    private Random rand;
    private float[] m_Chromosome = new float[CHROMOSOME_SIZE];
    private float m_fitness;
    
    public Gene(){};
    public Gene(float[] chromosome)
    {
        m_Chromosome = chromosome;
    }
    
    public void Initialize()
    {
        rand = new Random(System.currentTimeMillis());
        m_Chromosome = new float[CHROMOSOME_SIZE];
        m_fitness = 0;
        
        for (int i = 0; i < CHROMOSOME_SIZE; i+=2) 
        {
            m_Chromosome[i] = rand.nextFloat();
            float nextValue = rand.nextFloat();
            while(m_Chromosome[i]> m_Chromosome[i+1])
            {
                nextValue = rand.nextFloat();                
            }
            m_Chromosome[i+1] = nextValue;
        }
    }
    
    public String getPhenotype(){
        String phenotype = "";
        return phenotype;
    }
    
    public void updateFitness(){
        //count fitness (based on phenotype?) here.
        //set fitness based on this value
    }
    
    //decode chromosome
    public float getHuntDistance()
    {
        throw new NotImplementedException();
    }
    
    public float getLookForHealthHealth()
    {
         throw  new NotImplementedException();
    }
    
    //get fitness function(s)
    public float getFitness(){ return m_fitness;}
    // get value at chromosome index
    public float getChromosomeElement(int i){ return m_Chromosome[i]; }
    // set value at chromosome index
    public void setChromosomeElement(int i, float value){ m_Chromosome[i] = value; }
    // mutation
    public void mutate(){
        for(int i=0; i<CHROMOSOME_SIZE; i++){
            if(new Random().nextFloat() < GeneManager.MUTATION_RATE){
                m_Chromosome[i] = new Random().nextFloat();
            }
        }
    }
    // change chromosome
    public void setChromosome(float[] newChromosome){
        m_Chromosome = newChromosome;
    }
    
    public float[] getChromosome(){
        return m_Chromosome;
    }
    
    /**
     * Override method for sorting genes in the population, depending on their fitness.
     * @param otherGene gene to compare with
     * @return whether gene should be moved up or down in the hierarchy.
     */
    @Override
    public int compareTo(Object otherGene){
        //typecasting temporary Gene to compare with "this"
        Gene tempGene = (Gene)otherGene;
            
        //if 'this' fitness is worse than the other Gene's
        if(getFitness() < tempGene.getFitness()){
            //this is pushed back.
            return -1;
        }//if not:
        else if(getFitness() > tempGene.getFitness()){
            //then push to the front .
            return 1;
        }
        //if they're equal then nothing is done.
        return 0;
    }
}
